package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonEvaluateCountMapper;
import com.yungu.swift.common.model.dto.CommonEvaluateCountDto;
import com.yungu.swift.common.service.CommonEvaluateCountService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class CommonEvaluateCountServiceImpl extends BaseServiceImpl<CommonEvaluateCountDto> implements CommonEvaluateCountService {

    @Autowired
    private CommonEvaluateCountMapper commonEvaluateCountMapper;

    @Override
    protected IMapper<CommonEvaluateCountDto> getMapper() {
        return commonEvaluateCountMapper;
    }


    @Override
    public ResponseData<List<CommonEvaluateCountDto>> findListByDriverUuid(String driverUuid) {
        Map<String, Object> objectMap = MapUtils.build("driverUuid", driverUuid);
        objectMap.put("beforeDate", 1);
        return ResponseData.buildSuccessResponse(commonEvaluateCountMapper.findListByDriverUuid(objectMap));
    }

    @Override
    public ResponseData<Boolean> updEvaluateCount(String driverUuid, String commentId) {
        Set<String> toSet = org.springframework.util.StringUtils.commaDelimitedListToSet(commentId);
        Iterator<String> iterator = toSet.iterator();
        while (iterator.hasNext()) {
            String tagUuid = iterator.next();
            CommonEvaluateCountDto commonEvaluateCountDto;
            Map<String, Object> objectMap = MapUtils.build(4);
            objectMap.put("currentDate", true);
            objectMap.put("driverUuid", driverUuid);
            objectMap.put("tagUuid", tagUuid);
            List<CommonEvaluateCountDto> list = commonEvaluateCountMapper.list(objectMap);
            if (CollectionUtils.isNotEmpty(list)) {
                commonEvaluateCountDto = list.get(0);
                commonEvaluateCountDto.setUpdateOn(new Date());
            } else {
                commonEvaluateCountDto = new CommonEvaluateCountDto();
                commonEvaluateCountDto.setDriverUuid(driverUuid);
                commonEvaluateCountDto.setTagUuid(tagUuid);
                commonEvaluateCountDto.setTagName(tagUuid);
                commonEvaluateCountDto.setCreateOn(new Date());
            }
            commonEvaluateCountDto.setTagCount(ParamUtil.defaultIfNull(commonEvaluateCountDto.getTagCount(), 0) + 1);
            if (StringUtils.isNotEmpty(commonEvaluateCountDto.getUuid())) {
                return ResponseData.buildSuccessResponse("统计标签成功", commonEvaluateCountMapper.edit(commonEvaluateCountDto) > 0);
            } else {
                commonEvaluateCountDto.setUuid(StringUtils.buildUUID());
                return ResponseData.buildSuccessResponse("统计标签成功", commonEvaluateCountMapper.add(commonEvaluateCountDto) > 0);
            }
        }
        return ResponseData.buildSuccessResponse("统计标签成功", Boolean.TRUE);
    }
}